// pages/orderEquipment/orderEquipment.js
const app = getApp();
import CommonService from '../../utils/service/commonService.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../utils/util');
import Base64 from '../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //列表
    list:[],
    //无数据
    showNothing:false,
    //传参
    obj:null,
    //项目选择框
    showProject:false,
    //项目选择数据
    projectList:[],
    //选择的项目id
    projectId: '',
    //用户
    user:null,
    //当前项目名
    currentName:'',
    //当前订单设备表的id
    currentId:null,
    //创建项目提示框
    showTips:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:app.globalData.userInfo
    })

    if(options.obj){
      console.log('订单设备传参');
      console.log(JSON.parse(options.obj));
      this.setData({
        obj:JSON.parse(options.obj)
      })

      //获取列表数据
      this.getList();
    }
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: false,
      className: 'deliverGoodsItems',
      data: {
        searchAll: true,
        deliverGoodId:this.data.obj.id,
        selectFields: ['id','pic','equipmentName','configName','projectObj','price','imei','eId','projectId','projectName']
      }
    }

    CommonService.goFindList(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        console.log('设备列表');
        console.log(result);

        result.forEach(item => {
          let index = item.pic.indexOf("?");
          if(index!=-1){
            item.pic = item.pic.substring(0, index);
          }
        });

        this.setData({
          list: result
        })
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      Toast.clear();
      this.setData({
        showNothing: true
      })
    })
  },

  //跳转添加设备
  goToAddEquipment(e){
    let detail=e.currentTarget.dataset.detail;
    let objs=this.data.obj;
    let obj={
      id:detail.id,
      eId:detail.eId,
      pic: detail.pic,
      memberId: objs.memberId,
      dealerId: objs.dealerId ? objs.dealerId : null,
      numbers:objs.numbers,
      orderId:objs.id,
      equipmentName:detail.equipmentName,
      projectId:detail.projectId ? detail.projectId : null
    }
    wx.navigateTo({
      url: "/pages/equipmentAdd/equipmentAdd?factoryBind="+JSON.stringify(obj)
    })
  },

  //跳转物联网定位信息
  goToMap(e){
    let detail=e.currentTarget.dataset.detail;
    wx.navigateTo({
      url: "/pages/iotEquipmentDetail/iotEquipmentDetail?imei="+detail.imei
    })
  },

  //内勤人员确认项目
  onShowProject(e){
    let detail=e.currentTarget.dataset.detail;
    this.setData({
      showProject: true,
      currentId:detail.id
    })
    //如果有项目名
    if(detail.projectName){
      let name=detail.projectName.trim();
      if(name){
        this.setData({
          currentName: name
        })
        this.getProject();
      }else{
        this.setData({
          showTips: true
      })
      }
    }else{
      this.setData({
        showTips: true
      })
    }
  },

  //查询项目
  getProject(){
    this.setData({
      projectList: []
    })

    let param = {
      isAuth: false,
      className: 'depart',
      data: {
        searchAll:true,
        selectFields: ['id','name'],
        nbl:Base64.encode(util.timeFormatDay(new Date())),
      }
    }

    //名称精确查询
    if(this.data.currentName){
      param.data.name=this.data.currentName;
    }

    //名称模糊查询
    if(this.data.valueSearchProject){
      param.data.name='%'+this.data.valueSearchProject+'%';
    }

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let list=res.data.data.result;
        let arr=[];
        for(let i=0;i<list.length;i++){
          let obj={
            text:list[i].name,
            id:list[i].id
          }
          arr.push(obj);
        }
        this.setData({
          projectList: arr
        })
      }
    }).catch((error) => {
    })
  },

  //隐藏项目选择框
  onCloseProject(){
    this.setData({
			showProject: false
    })
  },

  //确认项目
  onConfirmProject(e){
    this.setData({
      projectId: e.detail.value.id
    })

    //保存项目id
    this.saveProject();
    //关闭选择框
    this.onCloseProject();
    //更新列表
    this.getList();
  },

  //保存项目id
  saveProject(){
    let param = {
      className: 'deliverGoodsItems',
      data: {
        filter: {
          id: this.data.currentId
        },
        update: {
          set: {
            projectId: this.data.projectId
          }
        }
      }
    }
    CommonService.goUpdate(param).then(res => {
    }).catch((error) => {
    })
  },

  //刷新页面
  onFefresh(){
    //关闭选择框
    this.onCloseProject();
    //更新列表
    this.getList();
  },

  //输入项目名
  onChangeProject(e){
    this.setData({
      valueSearchProject: e.detail
    })
    //查询项目
    this.getProject();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
